﻿using mozhi.smarterp.BasicData.Customers.Repositories;
using mozhi.smarterp.BasicData.Customers.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Guids;

namespace mozhi.smarterp.BasicData.Customers
{
    public class CustomerAppService : BasicDataAppService, ICustomerAppService
    {
        private ICustomerRepository _repository;
        private CustomerManager _manager;
        private IGuidGenerator _guidGenerator;

        public CustomerAppService(ICustomerRepository repository, CustomerManager manager, IGuidGenerator guidGenerator)
        {
            _repository = repository;
            _manager = manager;
            _guidGenerator = guidGenerator;
        }

        public async Task<PagedResultDto<CustomerListDto>> GetListAsync(PagedResultRequestDto filter)
        {
            var customers = await _repository.GetPagedListAsync(filter.SkipCount, filter.MaxResultCount, nameof(Customer.Code));

            var count = await _repository.GetCountAsync();

            var result = new PagedResultDto<CustomerListDto>()
            {
                Items = ObjectMapper.Map<List<Customer>, List<CustomerListDto>>(customers),
                TotalCount = count
            };

            return result;
        }

        public async Task<CustomerDto> GetAsync(Guid id)
        {
            var customer = await _repository.GetAsync(id);

            return ObjectMapper.Map<Customer, CustomerDto>(customer);
        }

        public async Task<CustomerDto> CreateAsync(CustomerCreateDto input)
        {
            var customer = await _manager.CreateAsync(_guidGenerator.Create(), input.Code, input.Name, input.Activated);

            customer.ChangeRegionAndCountry(input.Country, input.Region);

            customer.ChangeContactAddress(input.MailAddress, input.PostalCode, input.Telephone);


            input.ContactItems = input.ContactItems ?? new List<CustomerContactCreateItemDto>();

            foreach (var item in input.ContactItems)
            {
                var contactItem = new CustomerContactItem(_guidGenerator.Create(), customer.Id, item.Name, item.Telephone, item.Mobilephone, item.Email);

                customer.ContactItems.Add(contactItem);
            }

            customer = await _repository.InsertAsync(customer);

            return ObjectMapper.Map<Customer, CustomerDto>(customer);
        }

        public async Task<CustomerDto> UpdateAsync(CustomerUpdateDto input)
        {
            var customer = await _manager.UpdateAsync(input.Id, input.Code, input.Name, input.Activated);

            customer.ChangeRegionAndCountry(input.Country, input.Region);

            customer.ChangeContactAddress(input.MailAddress, input.PostalCode, input.Telephone);

            input.ContactItems = input.ContactItems ?? new List<CustomerContactUpdateItemDto>();

            var contactItems = new List<CustomerContactItem>();

            foreach (var item in input.ContactItems)
            {
                item.Id = item.Id == default ? _guidGenerator.Create() : item.Id;

                var contactItem = new CustomerContactItem(item.Id, customer.Id, item.Name, item.Telephone, item.Mobilephone, item.Email);

                contactItems.Add(contactItem);
            }

            customer.ChangeContactItems(contactItems);

            customer = await _repository.UpdateAsync(customer);

            return ObjectMapper.Map<Customer, CustomerDto>(customer);
        }

        public async Task DeleteAsync(Guid id)
        {
            var customer = await _repository.GetAsync(id);

            if (customer == null)
                throw new BusinessException($"客户：{id}不存在", "100000");

            await _repository.DeleteAsync(id);
        }

    }
}
